/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.common.helper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.eyaomall.wxmall.common.enums.MemberCookieEnums;
import com.lycheeframework.core.common.cookie.SessionManager;
import com.lycheeframework.core.common.util.StringUtil;
import com.lycheeframework.core.common.util.encrypt.MD5;
import com.lycheeframework.core.common.util.encrypt.SHA256;

public class MemberHelper {
	private static boolean DEBUG = true;

	/**
	 * 获取会员ID
	 * 
	 * @return
	 */
	public static Integer getId() {
		if (DEBUG) {
			return 3;
		}

		String memberId = getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_MEMBER_ID.getName(), true);
		if (StringUtil.isEmpty(memberId) || "null".equalsIgnoreCase(memberId)) {
			return 0;
		}
		return StringUtil.isEmpty(memberId) ? 0 : Integer.parseInt(memberId);
	}

	/**
	 * 获取会员OpenID
	 * 
	 * @return
	 */
	public static String getOpenId() {
		if (DEBUG) {
			return "oaKr8v4fqZwv1qZcNoBEDE-QO-0s";
		}

		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_OPEN_ID.getName(), true);
	}

	/**
	 * 获取会员OpenID
	 * 
	 * @return
	 */
	public static String getEId() {

		return (String) getRequest().getSession().getAttribute("eid");
	}

	/**
	 * 获取会员手机号码
	 * 
	 * @return
	 */
	public static String getPhone() {

		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_PHONE.getName(), true);
	}

	/**
	 * 获取会员昵称
	 * 
	 * @return
	 */
	public static String getNickName() {

		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_NICK_NAME.getName(), true);
	}

	/**
	 * 获取会员昵称
	 * 
	 * @return
	 */
	public static String getHeadImg() {

		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_HEAD_IMG.getName(), true);
	}

	/**
	 * 获取店铺ID
	 * 
	 * @return
	 */
	public static Integer getShopId() {
		if (DEBUG) {
			return 186;
		}

		String shopId = getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),
				MemberCookieEnums._MALL_FORM_SHOP_ID.getName(), true);
		if (StringUtil.isEmpty(shopId) || "null".equalsIgnoreCase(shopId)) {
			return 0;
		}
		return StringUtil.isEmpty(shopId) ? 0 : Integer.parseInt(shopId);
	}

	/**
	 * 加密密码明文
	 * 
	 * @param passwd
	 * @return
	 */
	public static String getEntryPasswd(String passwd) {
		passwd = SHA256.encrypt(passwd);

		return MD5.encryptMD5(passwd);
	}

	public static HttpServletRequest getRequest() {

		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	public static HttpServletResponse getResponse() {

		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}

	private static SessionManager getManager() {

		return new SessionManager(getRequest());
	}

	public static void cleanAll() {
		getRequest().getSession().invalidate();
	}
}
